package com.sf.osms.dep.lock;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import com.sf.novatar.cluster.IKeepAliveAndLockResolver;
import com.sf.novatar.context.AppContext;
import com.sf.novatar.util.ThreadUtils;

@Component
public class ClusterSyncHeart extends Thread implements InitializingBean, BeanFactoryAware{
	
	private BeanFactory beanFactory;
	private IKeepAliveAndLockResolver keepAliveAndLockResolver;
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Override
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		this.beanFactory = beanFactory;
		
	}
	
	@Override
	public void afterPropertiesSet() throws Exception {
		this.keepAliveAndLockResolver = this.beanFactory.getBean(IKeepAliveAndLockResolver.class);
		start();
	}
	
	 public void run(){
	    for (;;)
	    {
	      try
	      {
	        this.keepAliveAndLockResolver.updateAlive();
	      }
	      catch (Exception x)
	      {
	        this.logger.error("Keep alive update exception.", x);
	      }
	      finally
	      {
	        ThreadUtils.sleepNoException(10000L);
	      }
	    }
	  }
	
	 public static void main(String[] args) {
		 String instanceId = AppContext.getContext().getInstanceId();
		 System.out.println(instanceId);
	}
}
